package com.brainsz.android.silentmodetoggle;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class SilentModeToggleActivity extends Activity {
    /** Called when the activity is first created. */
	
	private AudioManager mAudioManager;

	private boolean mPhoneIsSilent;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        
        mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        
        checkIfPhoneIsSilent();
        
    }
    
    public void onClick (View view)
    {
		if (mPhoneIsSilent)
    	{
    		mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    		mPhoneIsSilent = false;
    		
    	} else {
    		mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    		mPhoneIsSilent = true;
    	}
    	toggleUi();
    }
	
    
    private void checkIfPhoneIsSilent() {
		int ringerMode = mAudioManager.getRingerMode();
		if (ringerMode == AudioManager.RINGER_MODE_SILENT)
		{
			mPhoneIsSilent = true;
		} else {
			mPhoneIsSilent = false;
		}
    	
		
	}

	private void toggleUi() {
		ImageView iv = (ImageView)findViewById(R.id.phone_icon);
		Drawable changePhoneImage;
		
		if (mPhoneIsSilent == true)
		{
			changePhoneImage = getResources().getDrawable(R.drawable.phone_silent);
		} else {
			changePhoneImage = getResources().getDrawable(R.drawable.phone_on);
		}
		iv.setImageDrawable(changePhoneImage);
		
	}
	
	protected void onResume()
	{
		super.onResume();
		checkIfPhoneIsSilent();
		toggleUi();
	}
	
}